package passhashr;

import java.util.EmptyStackException;
import java.util.Scanner;
import java.io.File;

public class PassHashR {
	static Scanner sc = new Scanner(System.in);
	/**
	 * Simple Pass Hashr
	 */
	public static void main(String[] args) {
		PassHashR pshr = new PassHashR();
		inout io = new inout();
		System.out.print("Did you want to encode or decode?");
		String create = sc.nextLine();
		
		if(create.startsWith("e") || create.startsWith("E")){
			System.out.print("Enter Text To convert: ");
			String convert = sc.nextLine();
			if(io.isSecurityValid()){
				System.out.print("Security File found; Would you like to use it? ");
				String yesno = sc.nextLine();
				if(yesno.startsWith("n") || yesno.startsWith("N")){
					io.deleteSecurity();
					System.out.println("How Encrypted? (0-2,147,483,647) : ");
					System.out.println("Large number can increase generate and decode times. ");
					int encrypted = sc.nextInt();
					String SuperString = pshr.encode(convert, encrypted);
					System.out.println(SuperString);
				}
				else{
					String[] letters = io.ReadFile("securitycode");
					String SuperString = pshr.encode(convert, letters);
					System.out.println(SuperString);
				}
			}else{
				System.out.println("How Encrypted? (0-2,147,483,647) : ");
				System.out.println("Large number can increase generate and decode times. ");
				int encrypted = sc.nextInt();
				String SuperString = pshr.encode(convert, encrypted);
				System.out.println(SuperString);
			}
		}
		else if(create.startsWith("d") || create.startsWith("D")){
			System.out.print("Paste in what to Decode: ");
			String decode = sc.nextLine();
			String SuperString = pshr.decode(decode);
			System.out.println("Here is your code decoded: ");
			System.out.println(SuperString);
		}
}
	public String encode(String convert, int Encrypted){
		RandomGen rg = new RandomGen();
		inout io = new inout();
		StringBuilder SuperString = new StringBuilder("");
		String[] letters = rg.generatecodes(Encrypted);
		char let;
		int size = convert.length();
		for(int i = 0; i < size; i++){
			let = convert.charAt(i);
			try{
				SuperString.append(encodecheck(let, letters));
			}catch(EmptyStackException e){
				System.out.println("There is an error in your text near " + i +".");
				break;
			}
		}
		io.DumptoFile(letters, "securitycode");
		return SuperString.toString();
	}
	public String encode(String convert, String[] letters){
		StringBuilder SuperString = new StringBuilder("");
		char let;
		int size = convert.length();
		for(int i = 0; i < size; i++){
			let = convert.charAt(i);
			try{
				SuperString.append(encodecheck(let, letters));
			}catch(EmptyStackException e){
				System.out.println("There is an error in your text near " + i +".");
				break;
			}
		}		
		return SuperString.toString();
	}
	public String encodecheck(int let, String[] letters){
		StringBuilder SuperString = new StringBuilder("");
		switch(let){
                    case (char)9: SuperString.append(letters[0]); break;
                    case (char)11: SuperString.append(letters[1]); break;
                    case (char)13: SuperString.append(letters[2]); break;
                    case (char)32: SuperString.append(letters[3]); break;
                    case (char)33: SuperString.append(letters[4]); break;
                    case (char)35: SuperString.append(letters[5]); break;
                    case (char)36: SuperString.append(letters[6]); break;
                    case (char)37: SuperString.append(letters[7]); break;
                    case (char)38: SuperString.append(letters[8]); break;
                    case (char)39: SuperString.append(letters[9]); break;
                    case (char)40: SuperString.append(letters[10]); break;
                    case (char)41: SuperString.append(letters[11]); break;
                    case (char)42: SuperString.append(letters[12]); break;
                    case (char)43: SuperString.append(letters[13]); break;
                    case (char)44: SuperString.append(letters[14]); break;
                    case (char)45: SuperString.append(letters[15]); break;
                    case (char)46: SuperString.append(letters[16]); break;
                    case (char)47: SuperString.append(letters[17]); break;
                    case (char)48: SuperString.append(letters[18]); break;
                    case (char)49: SuperString.append(letters[19]); break;
                    case (char)50: SuperString.append(letters[20]); break;
                    case (char)51: SuperString.append(letters[21]); break;
                    case (char)52: SuperString.append(letters[22]); break;
                    case (char)53: SuperString.append(letters[23]); break;
                    case (char)54: SuperString.append(letters[24]); break;
                    case (char)55: SuperString.append(letters[25]); break;
                    case (char)56: SuperString.append(letters[26]); break;
                    case (char)57: SuperString.append(letters[27]); break;
                    case (char)58: SuperString.append(letters[28]); break;
                    case (char)59: SuperString.append(letters[29]); break;
                    case (char)60: SuperString.append(letters[30]); break;
                    case (char)61: SuperString.append(letters[31]); break;
                    case (char)62: SuperString.append(letters[32]); break;
                    case (char)63: SuperString.append(letters[33]); break;
                    case (char)64: SuperString.append(letters[34]); break;
                    case (char)65: SuperString.append(letters[35]); break;
                    case (char)66: SuperString.append(letters[36]); break;
                    case (char)67: SuperString.append(letters[37]); break;
                    case (char)68: SuperString.append(letters[38]); break;
                    case (char)69: SuperString.append(letters[39]); break;
                    case (char)70: SuperString.append(letters[40]); break;
                    case (char)71: SuperString.append(letters[41]); break;
                    case (char)72: SuperString.append(letters[42]); break;
                    case (char)73: SuperString.append(letters[43]); break;
                    case (char)74: SuperString.append(letters[44]); break;
                    case (char)75: SuperString.append(letters[45]); break;
                    case (char)76: SuperString.append(letters[46]); break;
                    case (char)77: SuperString.append(letters[47]); break;
                    case (char)78: SuperString.append(letters[48]); break;
                    case (char)79: SuperString.append(letters[49]); break;
                    case (char)80: SuperString.append(letters[50]); break;
                    case (char)81: SuperString.append(letters[51]); break;
                    case (char)82: SuperString.append(letters[52]); break;
                    case (char)83: SuperString.append(letters[53]); break;
                    case (char)84: SuperString.append(letters[54]); break;
                    case (char)85: SuperString.append(letters[55]); break;
                    case (char)86: SuperString.append(letters[56]); break;
                    case (char)87: SuperString.append(letters[57]); break;
                    case (char)88: SuperString.append(letters[58]); break;
                    case (char)89: SuperString.append(letters[59]); break;
                    case (char)90: SuperString.append(letters[60]); break;
                    case (char)91: SuperString.append(letters[61]); break;
                    case (char)93: SuperString.append(letters[62]); break;
                    case (char)94: SuperString.append(letters[63]); break;
                    case (char)95: SuperString.append(letters[64]); break;
                    case (char)96: SuperString.append(letters[65]); break;
                    case (char)97: SuperString.append(letters[66]); break;
                    case (char)98: SuperString.append(letters[67]); break;
                    case (char)99: SuperString.append(letters[68]); break;
                    case (char)100: SuperString.append(letters[69]); break;
                    case (char)101: SuperString.append(letters[70]); break;
                    case (char)102: SuperString.append(letters[71]); break;
                    case (char)103: SuperString.append(letters[72]); break;
                    case (char)104: SuperString.append(letters[73]); break;
                    case (char)105: SuperString.append(letters[74]); break;
                    case (char)106: SuperString.append(letters[75]); break;
                    case (char)107: SuperString.append(letters[76]); break;
                    case (char)108: SuperString.append(letters[77]); break;
                    case (char)109: SuperString.append(letters[78]); break;
                    case (char)110: SuperString.append(letters[79]); break;
                    case (char)111: SuperString.append(letters[80]); break;
                    case (char)112: SuperString.append(letters[81]); break;
                    case (char)113: SuperString.append(letters[82]); break;
                    case (char)114: SuperString.append(letters[83]); break;
                    case (char)115: SuperString.append(letters[84]); break;
                    case (char)116: SuperString.append(letters[85]); break;
                    case (char)117: SuperString.append(letters[86]); break;
                    case (char)118: SuperString.append(letters[87]); break;
                    case (char)119: SuperString.append(letters[88]); break;
                    case (char)120: SuperString.append(letters[89]); break;
                    case (char)121: SuperString.append(letters[90]); break;
                    case (char)122: SuperString.append(letters[91]); break;
                    case (char)123: SuperString.append(letters[92]); break;
                    case (char)124: SuperString.append(letters[93]); break;
                    case (char)125: SuperString.append(letters[94]); break;
                    case (char)126: SuperString.append(letters[95]); break;

                    default	:throw new EmptyStackException();
                }
		return SuperString.toString();
	}

	public String decode(String Decode){
		inout io = new inout();
		String[] letters = io.ReadFile("securitycode");
		String SuperString = "";
			
		SuperString = decodecheck(Decode, letters);	
		return SuperString;
	}
        public String decode(String Decode, File f){
		inout io = new inout();
		String[] letters = io.ReadFile(f);
		String SuperString = "";
			
		SuperString = decodecheck(Decode, letters);	
		return SuperString;
	}
	
	public String decodecheck(String decode, String[] letters){
		int size = letters[0].length(), characters = decode.length() / size, a = 0, b = a + size;
		StringBuilder SuperString = new StringBuilder("");
		for(int i = 0; i < characters; i++){
                    if ((decode.substring(a,b)).equals(letters[0])) SuperString.append((char)9);
                    else if ((decode.substring(a,b)).equals(letters[1])) SuperString.append((char)11);
                    else if ((decode.substring(a,b)).equals(letters[2])) SuperString.append((char)13);
                    else if ((decode.substring(a,b)).equals(letters[3])) SuperString.append((char)32);
                    else if ((decode.substring(a,b)).equals(letters[4])) SuperString.append((char)33);
                    else if ((decode.substring(a,b)).equals(letters[5])) SuperString.append((char)35);
                    else if ((decode.substring(a,b)).equals(letters[6])) SuperString.append((char)36);
                    else if ((decode.substring(a,b)).equals(letters[7])) SuperString.append((char)37);
                    else if ((decode.substring(a,b)).equals(letters[8])) SuperString.append((char)38);
                    else if ((decode.substring(a,b)).equals(letters[9])) SuperString.append((char)39);
                    else if ((decode.substring(a,b)).equals(letters[10])) SuperString.append((char)40);
                    else if ((decode.substring(a,b)).equals(letters[11])) SuperString.append((char)41);
                    else if ((decode.substring(a,b)).equals(letters[12])) SuperString.append((char)42);
                    else if ((decode.substring(a,b)).equals(letters[13])) SuperString.append((char)43);
                    else if ((decode.substring(a,b)).equals(letters[14])) SuperString.append((char)44);
                    else if ((decode.substring(a,b)).equals(letters[15])) SuperString.append((char)45);
                    else if ((decode.substring(a,b)).equals(letters[16])) SuperString.append((char)46);
                    else if ((decode.substring(a,b)).equals(letters[17])) SuperString.append((char)47);
                    else if ((decode.substring(a,b)).equals(letters[18])) SuperString.append((char)48);
                    else if ((decode.substring(a,b)).equals(letters[19])) SuperString.append((char)49);
                    else if ((decode.substring(a,b)).equals(letters[20])) SuperString.append((char)50);
                    else if ((decode.substring(a,b)).equals(letters[21])) SuperString.append((char)51);
                    else if ((decode.substring(a,b)).equals(letters[22])) SuperString.append((char)52);
                    else if ((decode.substring(a,b)).equals(letters[23])) SuperString.append((char)53);
                    else if ((decode.substring(a,b)).equals(letters[24])) SuperString.append((char)54);
                    else if ((decode.substring(a,b)).equals(letters[25])) SuperString.append((char)55);
                    else if ((decode.substring(a,b)).equals(letters[26])) SuperString.append((char)56);
                    else if ((decode.substring(a,b)).equals(letters[27])) SuperString.append((char)57);
                    else if ((decode.substring(a,b)).equals(letters[28])) SuperString.append((char)58);
                    else if ((decode.substring(a,b)).equals(letters[29])) SuperString.append((char)59);
                    else if ((decode.substring(a,b)).equals(letters[30])) SuperString.append((char)60);
                    else if ((decode.substring(a,b)).equals(letters[31])) SuperString.append((char)61);
                    else if ((decode.substring(a,b)).equals(letters[32])) SuperString.append((char)62);
                    else if ((decode.substring(a,b)).equals(letters[33])) SuperString.append((char)63);
                    else if ((decode.substring(a,b)).equals(letters[34])) SuperString.append((char)64);
                    else if ((decode.substring(a,b)).equals(letters[35])) SuperString.append((char)65);
                    else if ((decode.substring(a,b)).equals(letters[36])) SuperString.append((char)66);
                    else if ((decode.substring(a,b)).equals(letters[37])) SuperString.append((char)67);
                    else if ((decode.substring(a,b)).equals(letters[38])) SuperString.append((char)68);
                    else if ((decode.substring(a,b)).equals(letters[39])) SuperString.append((char)69);
                    else if ((decode.substring(a,b)).equals(letters[40])) SuperString.append((char)70);
                    else if ((decode.substring(a,b)).equals(letters[41])) SuperString.append((char)71);
                    else if ((decode.substring(a,b)).equals(letters[42])) SuperString.append((char)72);
                    else if ((decode.substring(a,b)).equals(letters[43])) SuperString.append((char)73);
                    else if ((decode.substring(a,b)).equals(letters[44])) SuperString.append((char)74);
                    else if ((decode.substring(a,b)).equals(letters[45])) SuperString.append((char)75);
                    else if ((decode.substring(a,b)).equals(letters[46])) SuperString.append((char)76);
                    else if ((decode.substring(a,b)).equals(letters[47])) SuperString.append((char)77);
                    else if ((decode.substring(a,b)).equals(letters[48])) SuperString.append((char)78);
                    else if ((decode.substring(a,b)).equals(letters[49])) SuperString.append((char)79);
                    else if ((decode.substring(a,b)).equals(letters[50])) SuperString.append((char)80);
                    else if ((decode.substring(a,b)).equals(letters[51])) SuperString.append((char)81);
                    else if ((decode.substring(a,b)).equals(letters[52])) SuperString.append((char)82);
                    else if ((decode.substring(a,b)).equals(letters[53])) SuperString.append((char)83);
                    else if ((decode.substring(a,b)).equals(letters[54])) SuperString.append((char)84);
                    else if ((decode.substring(a,b)).equals(letters[55])) SuperString.append((char)85);
                    else if ((decode.substring(a,b)).equals(letters[56])) SuperString.append((char)86);
                    else if ((decode.substring(a,b)).equals(letters[57])) SuperString.append((char)87);
                    else if ((decode.substring(a,b)).equals(letters[58])) SuperString.append((char)88);
                    else if ((decode.substring(a,b)).equals(letters[59])) SuperString.append((char)89);
                    else if ((decode.substring(a,b)).equals(letters[60])) SuperString.append((char)90);
                    else if ((decode.substring(a,b)).equals(letters[61])) SuperString.append((char)91);
                    else if ((decode.substring(a,b)).equals(letters[62])) SuperString.append((char)93);
                    else if ((decode.substring(a,b)).equals(letters[63])) SuperString.append((char)94);
                    else if ((decode.substring(a,b)).equals(letters[64])) SuperString.append((char)95);
                    else if ((decode.substring(a,b)).equals(letters[65])) SuperString.append((char)96);
                    else if ((decode.substring(a,b)).equals(letters[66])) SuperString.append((char)97);
                    else if ((decode.substring(a,b)).equals(letters[67])) SuperString.append((char)98);
                    else if ((decode.substring(a,b)).equals(letters[68])) SuperString.append((char)99);
                    else if ((decode.substring(a,b)).equals(letters[69])) SuperString.append((char)100);
                    else if ((decode.substring(a,b)).equals(letters[70])) SuperString.append((char)101);
                    else if ((decode.substring(a,b)).equals(letters[71])) SuperString.append((char)102);
                    else if ((decode.substring(a,b)).equals(letters[72])) SuperString.append((char)103);
                    else if ((decode.substring(a,b)).equals(letters[73])) SuperString.append((char)104);
                    else if ((decode.substring(a,b)).equals(letters[74])) SuperString.append((char)105);
                    else if ((decode.substring(a,b)).equals(letters[75])) SuperString.append((char)106);
                    else if ((decode.substring(a,b)).equals(letters[76])) SuperString.append((char)107);
                    else if ((decode.substring(a,b)).equals(letters[77])) SuperString.append((char)108);
                    else if ((decode.substring(a,b)).equals(letters[78])) SuperString.append((char)109);
                    else if ((decode.substring(a,b)).equals(letters[79])) SuperString.append((char)110);
                    else if ((decode.substring(a,b)).equals(letters[80])) SuperString.append((char)111);
                    else if ((decode.substring(a,b)).equals(letters[81])) SuperString.append((char)112);
                    else if ((decode.substring(a,b)).equals(letters[82])) SuperString.append((char)113);
                    else if ((decode.substring(a,b)).equals(letters[83])) SuperString.append((char)114);
                    else if ((decode.substring(a,b)).equals(letters[84])) SuperString.append((char)115);
                    else if ((decode.substring(a,b)).equals(letters[85])) SuperString.append((char)116);
                    else if ((decode.substring(a,b)).equals(letters[86])) SuperString.append((char)117);
                    else if ((decode.substring(a,b)).equals(letters[87])) SuperString.append((char)118);
                    else if ((decode.substring(a,b)).equals(letters[88])) SuperString.append((char)119);
                    else if ((decode.substring(a,b)).equals(letters[89])) SuperString.append((char)120);
                    else if ((decode.substring(a,b)).equals(letters[90])) SuperString.append((char)121);
                    else if ((decode.substring(a,b)).equals(letters[91])) SuperString.append((char)122);
                    else if ((decode.substring(a,b)).equals(letters[92])) SuperString.append((char)123);
                    else if ((decode.substring(a,b)).equals(letters[93])) SuperString.append((char)124);
                    else if ((decode.substring(a,b)).equals(letters[94])) SuperString.append((char)125);
                    else if ((decode.substring(a,b)).equals(letters[95])) SuperString.append((char)126);

			else { 
				SuperString.replace(0, SuperString.length(),"Stop, your code has some errors!"); break;
			}
			a = b;
			b += size;
		}
		
		return SuperString.toString();
	}
}
